package beans;

import models.Article;
import models.User;

import java.util.ArrayList;
import java.util.List;

public class UserBean {
	public String id;
	
	public String nickname;
	
	public String address;
	
	public int sex;

	public String email;

	public String job;
	
	public int fansNum;

	public long articleCount;

	public String url;

	public List<ArticleBean> articleList;
	
	public static UserBean bulid(User userinfo){
		UserBean userbean = new UserBean();
        userbean.id = userinfo.id;
		userbean.address = userinfo.address;
		userbean.nickname = userinfo.nickname;
		userbean.email = userinfo.email;
		userbean.sex = userinfo.sex;
		userbean.fansNum = userinfo.fansNum;
		userbean.job = userinfo.workArea;
		userbean.url = userinfo.getPhoto();

		List<Article> articleList = Article.find("authorId = ? and isDelete != 1 order by createTime desc", userinfo.id).fetch(3);
		userbean.articleCount = Article.count("authorId = ? and isDelete != 1 order by createTime desc", userinfo.id);
        userbean.articleList = ArticleBean.buildList(articleList);
		return userbean;
	}
	
	public static List<UserBean> bulidList(List<User> userlist){
		List<UserBean> userBeanList = new ArrayList<UserBean>();
		for(int item =0 ; item < userlist.size(); item ++){
			User userinfo = userlist.get(item);
			UserBean userbean = UserBean.bulid(userinfo);
			userBeanList.add(userbean);
		}
		return userBeanList;
	}
}
